﻿@using JavaDevToys.Rcl.Service
@using JavaDevToys.Rcl.Service.Impl
@using Models
@page "/JavaModel"



<MAlert Dismissible
Type="AlertTypes.Error" 
@bind-Value="_alert">
    @errMsg
</MAlert>
<h3>模型生成</h3>
<MContainer Fluid>
    <MRow Align="@AlignTypes.Center">
        <MCol Cols="6">
            <div class="d-flex pt-6">
                <MSelect Items="settingInfo.JavaProjectInfoList"
                @bind-Value="@selectedJavaProjectName"
                ItemText="u => u.Desc"
                ItemValue="u => u.Name"
                Filled
                Label="选择工程"
                FullWidth="false"
                Dense></MSelect>
                <MButton Color="primary" Class="mt-2  ml-9" OnClick="Analysis">解析</MButton>
                <CompareCode ShowDialog="@showCompareCode" ShowDialogChanged="OnShowDialogChanged" NewMothedDict="newMothedDict">
                    <ActivatorContent>
                        <MButton Color="primary" Class="mt-2  ml-9" OnClick="CreateJavaMode" Disabled="apiInfo==null">生成类</MButton>
                    </ActivatorContent>
                </CompareCode>
                
            </div>
        </MCol>
    </MRow>
    <MRow>
        <MSelect Items="requestPathList"
        @bind-Value="@selectedRequestPath"
        ItemText="u => u"
        ItemValue="u => u"
        Filled
        Label="Request输出路径"
        FullWidth="false"
        Dense></MSelect>
    </MRow>
    <MRow>
        <MSelect Items="responsePathList"
        @bind-Value="@selectedResponsePath"
        ItemText="u => u"
        ItemValue="u => u"
        Filled
        Label="Response输出路径"
        FullWidth="false"
        Dense></MSelect>
    </MRow>
    <MRow Align="@AlignTypes.Center">
        <MTextarea Clearable
        ClearIcon="mdi-close-circle"
        Label="复制表格内容到此"
        @bind-Value="javaText">
        </MTextarea>
    </MRow>
    <MRow Align="@AlignTypes.Center">
        <MList>
            @if (outFilePathList != null&&outFilePathList.Count>0)
            {
                <MButton Color="primary" Class="mt-2  ml-9" OnClick="AddGit">添加到git</MButton>
                @foreach (var item in outFilePathList)
                {
                    <MListItem>
                        <MListItemAction>
                            <MIcon>@item.Value</MIcon>
                        </MListItemAction>
                        <MListItemContent>
                            <MListItemTitle>@item.Key</MListItemTitle>
                        </MListItemContent>
                    </MListItem>
                }
            }
        </MList>
    </MRow>
</MContainer>
@code {
    private bool _alert = false;
    private string errMsg = string.Empty;
    private string selectedJavaProjectName;
    private string javaText;
    private SettingInfo settingInfo;
    [Inject]
    public IDirectoryPicker directoryPicker { get; set; }
    [Inject]
    public IJavaCodeGenerator javaCodeGenerator { get; set; }
    [Inject]
    public IApiInfoParser apiInfoParser { get; set; }
    [Inject]
    public IJavaProjectService javaProjectService { get; set; }
    [Inject]
    public ISettingService settingService { get; set; }
    private Dictionary<string,string> outFilePathList;
    private ApiInfo apiInfo;
    private List<string> requestPathList = new List<string>();
    private string selectedRequestPath;

    private List<string> responsePathList = new List<string>();
    private string selectedResponsePath;

    private Dictionary<string, string> newMothedDict = new Dictionary<string, string>();
    private bool showCompareCode = false;
    private Guid CompareCodekey = Guid.NewGuid();
    protected override void OnInitialized()
    {
        base.OnInitialized();
        settingInfo = settingService.GetSettings(0);
    }

    private void OnShowDialogChanged(bool value)
    {
        showCompareCode = value;
    }

    private void Analysis(){
        JavaProjectInfo javaProjectInfo = settingInfo.JavaProjectInfoList.FirstOrDefault(f => f.Name == selectedJavaProjectName);
        if (javaProjectInfo == null)
        {
            errMsg = "请选择工程项目";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaProjectInfo.FullPath))
        {
            errMsg = "项目路径未设置，请点击右上角设置按钮进行设置";
            _alert = true;
            return;
        }
        if (string.IsNullOrWhiteSpace(javaText))
        {
            errMsg = "内容不能为空";
            _alert = true;
            return;
        }
        // 解析内容
        apiInfo = apiInfoParser.Parse(javaText);
        var requestPath=directoryPicker.SearchAllDirectory(javaProjectInfo.FullPath, "request");
        var responsePath = directoryPicker.SearchAllDirectory(javaProjectInfo.FullPath, "response");
        requestPathList = requestPath.Select(s => Path.Combine(s, apiInfo.ClassName.ToLower())).ToList();
        responsePathList = responsePath.Select(s => Path.Combine(s, apiInfo.ClassName.ToLower())).ToList();
        selectedRequestPath = requestPathList?.FirstOrDefault();
        selectedResponsePath = responsePathList?.FirstOrDefault();
    }

    private void CreateJavaMode()
    {
        // 解析内容
        // ApiInfo apiInfo = apiInfoParser.Parse(javaText);
        // 生成Java类
        JavaProjectInfo javaProjectInfo = settingInfo.JavaProjectInfoList.FirstOrDefault(f => f.Name == selectedJavaProjectName);
        javaProjectInfo.RequestClassPath = selectedRequestPath;
        javaProjectInfo.ResponseClassPath = selectedResponsePath;
        var pathList=javaCodeGenerator.GenerateModel(javaProjectInfo, apiInfo);
        javaCodeGenerator.GenerateDtoModel(javaProjectInfo, apiInfo, out List<string> newFileList, out Dictionary<string, string> fixCodeDict);
        outFilePathList = new Dictionary<string, string>();
        foreach (var item in pathList)
        {
            outFilePathList.Add(item, "mdi-label");
        }
        foreach (var item in newFileList)
        {
            outFilePathList.Add(item, "mdi-label");
        }
        if(fixCodeDict.Count>0){
            newMothedDict = fixCodeDict;
            CompareCodekey = Guid.NewGuid();
            showCompareCode = true;
        }
        apiInfo = null;
    }

    private void AddGit()
    {
        foreach (var item in outFilePathList)
        {
            if (javaProjectService.AddGitRepository(item.Key, out string errMsg))
            {
                outFilePathList[item.Key] = "mdi-check";
            }
        }
    }
}
